package com.ecgsystem.panel;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ecgsystem.activity.R;
import com.ecgsystem.constants.Constants;

public class LazyAdapter extends BaseAdapter {

	private Activity activity;
	private ArrayList<HashMap<String, String>> data;
	private static LayoutInflater inflater = null;

	public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
		activity = a;
		data = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public static class ViewHolder {
		public TextView patientID;
		public TextView patientName;
		public TextView patientAge;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		ViewHolder holder;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.list_item, null);
			holder = new ViewHolder();
			holder.patientID = (TextView) vi
					.findViewById(R.id.textView_patientID);
			holder.patientName = (TextView) vi
					.findViewById(R.id.textView_patientName);
			holder.patientAge = (TextView) vi
					.findViewById(R.id.textView_patientAge);
			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}

		holder.patientID.setText(data.get(position).get(
				Constants.TAG_PATIENT_ID));
		holder.patientName.setText(data.get(position).get(
				Constants.TAG_PATIENT_NAME));
		holder.patientAge.setText(data.get(position).get(
				Constants.TAG_PATIENT_AGE));
		return vi;
	}
}